package com.smec.remes.module.query.picture.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.hwangjr.rxbus.Bus;
import com.smec.moblie.BR;
import com.smec.moblie.R;
import com.smec.moblie.databinding.LayoutRecyclerviewLadderBinding;
import com.smec.moblie.databinding.LayoutRecyclerviewPictureBinding;
import com.smec.remes.base.SmecBaseRecyclerViewHolder;
import com.smec.remes.bean.query.QueryMantr;
import com.smec.remes.bean.query.QueryPicture;
import com.smec.remes.util.CopyText;
import com.smec.remes.util.ImageDialog;

import java.util.ArrayList;

/**
 * Created by Administrator on 2018/1/8.
 */

public class SmecPictureAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder>  {

    private ArrayList<QueryPicture> arrayList=new ArrayList<>();
    private Context mcontext;
    private ImageDialog imageDialog;
    public SmecPictureAdapter(Context context) {
        this.mcontext=context;
    }

    public void setArrayList(ArrayList<QueryPicture> list){
        arrayList=list;
        notifyDataSetChanged();
    }

    public void clearList(){
        arrayList.clear();
        notifyDataSetChanged();
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutRecyclerviewPictureBinding layoutRecyclerviewPictureBinding=DataBindingUtil.inflate
                (LayoutInflater.from(parent.getContext()), R.layout.layout_recyclerview_picture,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                SmecBaseRecyclerViewHolder(layoutRecyclerviewPictureBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(layoutRecyclerviewPictureBinding);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, final int position) {
        LayoutRecyclerviewPictureBinding layoutRecyclerviewPictureBinding= (LayoutRecyclerviewPictureBinding) holder.getBinding();
        layoutRecyclerviewPictureBinding.setVariable(BR.querypicture,arrayList.get(position));
        layoutRecyclerviewPictureBinding.llCopy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CopyText._copy(arrayList.get(position).getMatnr(),mcontext);
                Toast.makeText(mcontext,"复制成功",Toast.LENGTH_SHORT).show();
            }
        });
        layoutRecyclerviewPictureBinding.llPicture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!TextUtils.isEmpty(arrayList.get(position).getDownloadUrl())){
                    imageDialog=new ImageDialog(mcontext,R.style.project_back);
                    ImageView imageView= (ImageView) imageDialog.getCustomView().findViewById(R.id.iv_image);
                    Glide.with(mcontext)
                            .load(arrayList.get(position).getDownloadUrl())
                            .error(R.drawable.smec_glide_error)
                            .into(imageView);
                    imageDialog.show();
                }else {
                    Toast.makeText(mcontext,"没有图片",Toast.LENGTH_SHORT).show();
                }
            }
        });
        holder.getBinding().executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }
}
